package com.iti.jets.gpts.service;

import com.iti.jets.gpts.pojo.AttributeType;
import com.iti.jets.gpts.pojo.Point;
import com.iti.jets.gpts.pojo.Terminal;
import com.iti.jets.gpts.pojo.TranService;
import com.iti.jets.gpts.pojo.TranServiceAttribute;
import com.iti.jets.gpts.pojo.TranServiceAttributeId;
import com.iti.jets.gpts.pojo.TranServiceRoute;
import com.iti.jets.gpts.pojo.TranServiceRouteId;
import com.iti.jets.gpts.pojo.TranServiceType;
import com.iti.jets.gpts.pojo.TranServiceTypeAttributeType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class TranServiceService {

    private WebResource webResource;
    private Client client;
    private static final String BASE_URI = "http://localhost:48651/GPTRP/rest";

    public TranServiceService() {
        com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
        client = Client.create(config);
        webResource = client.resource(BASE_URI).path("transervice");
    }

    public List<TranService> getAllTranService() throws UniformInterfaceException, JSONException {
        WebResource resource = webResource;
        resource = resource.path("getalltranservice");
        JSONArray array = resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(JSONArray.class);
        List<TranService> tranServices = new ArrayList<TranService>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject jSONObject = array.getJSONObject(i);
            TranService tranService = new TranService();
            tranService.setId(jSONObject.getInt("id"));
            tranService.setName(jSONObject.getString("name"));
            //t1
            JSONObject t1 = jSONObject.getJSONObject("t1");
            Terminal terminal = new Terminal();
            terminal.setIdPoint(t1.getInt("id"));
            Point point = new Point();
            point.setId(t1.getInt("id"));
            point.setName(t1.getString("name"));
            point.setLocation(t1.getString("location"));
            point.setIsStop(t1.getBoolean("stop"));
            terminal.setPoint(point);
            tranService.setTerminalByTerminal1(terminal);
            //t2
            JSONObject t2 = jSONObject.getJSONObject("t2");
            Terminal terminal2 = new Terminal();
            terminal2.setIdPoint(t2.getInt("id"));
            Point point2 = new Point();
            point2.setId(t2.getInt("id"));
            point2.setName(t2.getString("name"));
            point2.setLocation(t2.getString("location"));
            point2.setIsStop(t2.getBoolean("stop"));
            terminal2.setPoint(point2);
            tranService.setTerminalByTerminal2(terminal2);
            //tst
            JSONObject tst = jSONObject.getJSONObject("tst");
            TranServiceType tranServiceType = new TranServiceType();
            tranServiceType.setId(tst.getInt("id"));
            tranServiceType.setName(tst.getString("name"));
            int comments = jSONObject.getInt("comments");
            int feedback = jSONObject.getInt("feedback");
            for (int j = 0; j < comments; j++) {
                tranService.getCommentses().add(null);
            }
            for (int j = 0; j < feedback; j++) {
                tranService.getFeedbacks().add(null);
            }
            tranService.setTranServiceType(tranServiceType);
            tranServices.add(tranService);
        }

        return tranServices;
    }

    //end of get all ts
//add
    public String addTranService(TranService tranService) throws UniformInterfaceException, JSONException {
        WebResource resource = webResource;
        resource = resource.path("add");
        JSONObject jsonTS = new JSONObject();
        jsonTS.put("name", tranService.getName());
        JSONObject jsonTST = new JSONObject();
        jsonTST.put("id", tranService.getTranServiceType().getId());
        jsonTS.put("tst", jsonTST);
        JSONObject jsonT1 = new JSONObject();
        jsonT1.put("id", tranService.getTerminalByTerminal1().getPoint().getId());
        jsonTS.put("terminal1", jsonT1);
        JSONObject jsonT2 = new JSONObject();
        jsonT2.put("id", tranService.getTerminalByTerminal2().getPoint().getId());
        jsonTS.put("terminal2", jsonT2);
        JSONArray jSONAtsr = new JSONArray();
        Iterator iterator = tranService.getTranServiceRoutes().iterator();
        while (iterator.hasNext()) {
            TranServiceRoute tsr = (TranServiceRoute) iterator.next();
            JSONObject jsontsr = new JSONObject();
            JSONObject jsonpoint = new JSONObject();
            jsonpoint.put("id", tsr.getPoint().getId());
            jsontsr.put("point", jsonpoint);
            jsontsr.put("homeorder", tsr.getHomeOrder());
            jsontsr.put("awayorder", tsr.getAwayOrder());

            jSONAtsr.put(jsontsr);
        }
        jsonTS.put("tsr", jSONAtsr);
        JSONArray jsonatsa = new JSONArray();
        Iterator iterator1 = tranService.getTranServiceAttributes().iterator();
        while (iterator1.hasNext()) {
            TranServiceAttribute tsa = (TranServiceAttribute) iterator1.next();
            JSONObject jsontsa = new JSONObject();
            JSONObject jsontstat = new JSONObject();
            JSONObject jsonAt = new JSONObject();
            jsonAt.put("id", tsa.getTranServiceTypeAttributeType().getAttributeType().getId());
            jsontstat.put("at", jsonAt);
            jsontsa.put("tstat", jsontstat);
            jsontsa.put("value", tsa.getValue());
            jsonatsa.put(jsontsa);
        }
        jsonTS.put("tsa", jsonatsa);

        System.out.println(jsonTS.toString());
        return webResource.path("add").type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(String.class, jsonTS);

    }

    //end of add
    
    
    
// edit
    public String editTranService(TranService tranService) throws UniformInterfaceException, JSONException {
       
        JSONObject jsonTS = new JSONObject();
        jsonTS.put("name", tranService.getName());
        jsonTS.put("id", tranService.getId());
        JSONObject jsonTST = new JSONObject();
        jsonTST.put("id", tranService.getTranServiceType().getId());
        jsonTS.put("tst", jsonTST);
        JSONObject jsonT1 = new JSONObject();
        jsonT1.put("id", tranService.getTerminalByTerminal1().getPoint().getId());
        jsonTS.put("terminal1", jsonT1);
        JSONObject jsonT2 = new JSONObject();
        jsonT2.put("id", tranService.getTerminalByTerminal2().getPoint().getId());
        jsonTS.put("terminal2", jsonT2);
        JSONArray jSONAtsr = new JSONArray();
        
        Iterator iterator = tranService.getTranServiceRoutes().iterator();
        while (iterator.hasNext()) {
            TranServiceRoute tsr = (TranServiceRoute) iterator.next();
            JSONObject jsontsr = new JSONObject();
            JSONObject jsonpoint = new JSONObject();
            jsonpoint.put("id", tsr.getPoint().getId());
            jsontsr.put("point", jsonpoint);
            jsontsr.put("homeorder", tsr.getHomeOrder());
            jsontsr.put("awayorder", tsr.getAwayOrder());
            jSONAtsr.put(jsontsr);
        }
        
        jsonTS.put("tsr", jSONAtsr);
        
        
        JSONArray jsonatsa = new JSONArray();
        Iterator iterator1 = tranService.getTranServiceAttributes().iterator();
        while (iterator1.hasNext()) {
            TranServiceAttribute tsa = (TranServiceAttribute) iterator1.next();
            JSONObject jsontsa = new JSONObject();
            JSONObject jsontstat = new JSONObject();
            JSONObject jsonAt = new JSONObject();
            jsonAt.put("id", tsa.getTranServiceTypeAttributeType().getAttributeType().getId());
            jsontstat.put("at", jsonAt);
            jsontsa.put("tstat", jsontstat);
            jsontsa.put("value", tsa.getValue());
            jsonatsa.put(jsontsa);
        }
        jsonTS.put("tsa", jsonatsa);

        System.out.println(jsonTS.toString());
        return webResource.path("edit").type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(String.class, jsonTS);
       // return "done";
    }

    // end edit
    // get tsr
    public List<TranServiceRoute> gettsr(String id) throws UniformInterfaceException, JSONException {
        WebResource resource = webResource;
        resource = resource.path(java.text.MessageFormat.format("gettsr/{0}", new Object[]{id}));
        JSONArray get = resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(JSONArray.class);
        List<TranServiceRoute> tsrs = new ArrayList<TranServiceRoute>();
        for (int i = 0; i < get.length(); i++) {
            TranServiceRoute tranServiceRoute = new TranServiceRoute();
            Point point = new Point();
            JSONObject jsontsr = get.getJSONObject(i);
            JSONObject jsonpoint = jsontsr.getJSONObject("point");
            point.setId(jsonpoint.getInt("id"));
            point.setIsStop(jsonpoint.getBoolean("is_stop"));
            point.setLocation(jsonpoint.getString("location"));
            point.setName(jsonpoint.getString("name"));

            tranServiceRoute.setPoint(point);

            if (jsontsr.has("awayorder")) {

                tranServiceRoute.setAwayOrder(jsontsr.getInt("awayorder"));

            }
            if (jsontsr.has("homeorder")) {
                tranServiceRoute.setHomeOrder(jsontsr.getInt("homeorder"));
            }
            tranServiceRoute.setId(new TranServiceRouteId(Integer.parseInt(id), point.getId()));
            tsrs.add(tranServiceRoute);
        }
        return tsrs;
    }

    // end tsr
    //get get tsa
    public List<TranServiceAttribute> gettsa(String id) throws UniformInterfaceException, JSONException {
        WebResource resource = webResource;
        resource = resource.path(java.text.MessageFormat.format("gettsa/{0}", new Object[]{id}));
        JSONArray get = resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(JSONArray.class);
        List<TranServiceAttribute> tsas = new ArrayList<TranServiceAttribute>();
        for (int i = 0; i < get.length(); i++) {
            TranServiceAttribute tsa = new TranServiceAttribute();
            JSONObject jsontsa = (JSONObject) get.get(i);
            tsa.setValue(jsontsa.getString("value"));
            JSONObject jsontstat = jsontsa.getJSONObject("tstat");
            TranServiceTypeAttributeType tstat = new TranServiceTypeAttributeType();
            tstat.setId(jsontstat.getInt("id"));
            AttributeType at = new AttributeType();
            JSONObject jsonat = jsontstat.getJSONObject("at");
            at.setName(jsonat.getString("name"));
            at.setIsNumber(jsonat.getBoolean("is_number"));
            at.setId(jsonat.getInt("id"));
            tstat.setAttributeType(at);
            tsa.setTranServiceTypeAttributeType(tstat);
            tsa.setId(new TranServiceAttributeId(tstat.getId(), Integer.parseInt(id)));
            tsas.add(tsa);
        }
        return tsas;
    }
    // end tsa

    public void close() {
        client.destroy();
    }

}
